// ==UserScript==
// @name         智慧树征服者
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       WHT
// @match        https://studyh5.zhihuishu.com/*
// @icon         
// @grant        none
// ==/UserScript==


$(function(){
    main();
});

let status = 0;
let last_time = undefined;

function main() {
    try {
        window.setInterval(
            ()=>{
                if (status === 1) {
                    console.log("结束了");
                    jump_next_video();
                } else if (status === 2) {
                    console.log("暂停了");
                    document.getElementById("playButton").click();
                } else if (status === 3) {
                    console.log("不动了");
                    document.getElementById("vjs_container_html5_api").muted = true;
                    document.getElementsByClassName("videoArea")[0].click();
                }
                status = 0;
                set_speed();
                judge_skip_test();
                judge_status();
            }, 3000);
        console.log("征服者设置成功!");
    } catch (e) {
        console.log("征服者设置失败:" + e);
    }
}

function judge_skip_test() {
    var dialog = document.querySelector("div[aria-label=弹题测验]");
    if(dialog != null) {
        var topic_items = dialog.getElementsByClassName("topic-item");
        if(topic_items != null) {
            try {
                // topic_items.item(1).click();//万一有多选题呢
                topic_items.item(0).click();
            } catch (e) {
                console.log(e);
                return;
            }
            dialog.querySelector("button[aria-label=Close]").click();
            document.getElementById("playButton").click();
            console.log("征服者成功跳过题目！");
        } else {
            console.log("征服者无法处理！");
        }
    } else {
        console.log("征服者未弹出问题");
    }
}

function judge_status() {
    try {
        var c_time = document.getElementsByClassName("currentTime")[0].textContent;
        if (c_time === last_time) {
            console.log("征服者:视频播放不动了");
            status = 3;
        }
        last_time = c_time;
        videojs("vjs_container_html5_api").ready(function () {
            this.on("ended", function () {
                console.log("征服者:视频播放结束");
                status = 1;
            });
            this.on("pause", function(){
                console.log("征服者:视频播放暂停");
                status = 2;
            });
        });
    } catch (e) {
        console.log(e);
        console.log("征服者:判断视频状态错误");
    }
}

function set_speed() {
    try {
        if (document.getElementsByClassName("speedBox")
                .item(0).getElementsByTagName("span")
                .item(0).textContent != "X 1.5"
        ) {
            document.getElementsByClassName("speedTab speedTab15").item(0).click();
            console.log("征服者:1.5倍速设置成功!");
        }
    } catch (e) {
        console.log(e);
        console.log("征服者:1.5倍速设置失败!");
    }
}

function jump_next_video() {
    try {
        var current_play = document.getElementsByClassName('clearfix video current_play')[0];
        var video_list = document.getElementsByClassName("clearfix video");
        var P = false;
        for (var i = 0; i < video_list.length; ++i) {
            if (P) {
                video_list[i].click();
                return;
            }
            if (video_list[i] === current_play) {
                P = true;
            }
        }
    } catch (e) {
        console.log();
    }

}
